package nature;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.function.BiFunction;

/**
 * People类
 */
public class People {
    private String name;            //姓名
    private String sex;             //性别
    private String nationality;     //民族
    private String born;            //出生
    private String address;         //住址
    private String identity;        //身份证号

    public People(){}
    public People(HashMap<String, Object> peopleInfo) throws NoSuchFieldException, IllegalAccessException {
        this.set(peopleInfo);
    }

    /**
     *  获取个人信息方法
     * @return  个人信息HashMap
     * @throws IllegalAccessException
     */
    public HashMap<String, Object> get() throws IllegalAccessException {
        //返回数据
        return tool.GetAndSetField.getFieldValue(this);
    }

    /**
     * 获取个人信息方法
     * @param getFields 获取属性名
     * @return 个人信息HashMap
     * @throws IllegalAccessException
     */
    public HashMap<String, Object> get(String[] getFields) throws IllegalAccessException {
        //返回数据
        return tool.GetAndSetField.getFieldValue(this, getFields);
    }

    /**
     * 设置个人信息方法
     * @param peopleInfo 个人信息HashMap
     * @return 设置成功属性
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public ArrayList<String> set(HashMap<String, Object> peopleInfo) throws NoSuchFieldException, IllegalAccessException {
        BiFunction<String, Object, Boolean> biFunction = (fieldName, fieldValue) -> {   //检查函数
            HashMap<String, String> check = new HashMap<>(){{                           //信息检查
                put("sex", "(男|女)");
                put("name", "\\S{1,64}");
                put("address", "\\S{1,64}");
                put("born", "(18|19|20)\\d{2}-(0|1)\\d-(0|1|2|3)\\d");
                put("identity", "(([1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}(\\d|(X|x)))|([1-9]\\d{7}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}))");
                put("nationality", "(汉族|满族|蒙古族|回族|藏族|维吾尔族|苗族|彝族|壮族|布依族|侗族|瑶族|白族|土家族|哈尼族|哈萨克族|傣族|黎族|傈僳族|佤族|畲族|高山族|拉祜族|水族|东乡族|纳西族|景颇族|柯尔克孜族|土族|达斡尔族|仫佬族|羌族|布朗族|撒拉族|毛南族|仡佬族|锡伯族|阿昌族|普米族|朝鲜族|塔吉克族|怒族|乌孜别克族|俄罗斯族|鄂温克族|德昂族|保安族|裕固族|京族|塔塔尔族|独龙族|鄂伦春族|赫哲族|门巴族|珞巴族|基诺族)");
            }};
            //获取正则
            String pattern = check.get(fieldName);
            //返回检查结果
            return pattern != null && fieldValue instanceof String && ((String) fieldValue).matches(pattern);
        };
        //返回数据
        return tool.GetAndSetField.setFieldValue(this, peopleInfo, biFunction);
    }
}